package sky.cin.framework.network.http;

import sky.cin.framework.network.Utils;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Response {
    
    private String version;
    private int status;
    private Map<String, Object> header = new ConcurrentHashMap<>();;
    private String data;

    public void init() {
        this.version = "HTTP/1.1";
        this.status = 200;
        this.header.put("Content-Type", "text/html;charset=UTF-8");
        this.header.put("Keep-Alive", "timeout=60");
        this.header.put("Connection", "keep-alive");
        this.header.put("Date", Utils.generateDate());
        this.data = "";
    }

    public Response() {
        init();
    }

    public Response(String responseTxtOrData) {
        String[] responseTxtLine = responseTxtOrData.split("\r\n");
        if (responseTxtLine.length == 1) {
            init();
            this.data = responseTxtOrData;
            return;
        }
        // 解析响应行
        String[] responseLine = responseTxtLine[0].split(" ");
        this.version = responseLine[0];
        this.status = Integer.parseInt(responseLine[1]);
        // 解析响应头
        for (int index = 1; index < responseTxtLine.length; index++) {
            String line = responseTxtLine[index].trim();
            if ("".equals(line)) {
                break;
            }
            setHeader(line);
        }
        // 解析响应体
        int length = responseTxtLine.length;
        String spaceLine = responseTxtLine[length - 2].trim();
        String lastLine = responseTxtLine[length - 1];
        if ("".equals(spaceLine)) {
            this.data = lastLine;
        }
    }

    public Response(Response response) {
        setRequest(response);
    }

    public void setRequest(Response response) {
        this.version = response.version;
        this.status = response.status;
        this.header = response.header;
        this.data = response.data;
    }

    public void setVersion(String version) {
        this.version = version;
    }
    
    public void setData(String data) {
        this.data = data;
    }

    public void setHeaders(Map<String, Object> header) {
        this.header = header;
    }

    public void setHeader(String key, Object value) {
        this.header.remove(key);
        this.header.put(key, value);
    }

    public void setHeader(String line) {
        String[] kv = line.split(": ");
        this.header.put(kv[0], kv[1]);
    }

    public void setStatus(int status) {
        this.status = status;
    }

    protected Response getResponse() {
        return this;
    }

    public String getVersion() {
        return version;
    }
    
    public int getStatus() {
        return status;
    }
    
    public Object getHeader(String key) {
        return header.get(key);
    }
    
    public String getData() {
        return data;
    }
    
    public void write(String msg) {
        this.data = msg;
        int ref = msg.split("\"").length - 1;
        this.setHeader("Content-Length", (new String(msg.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)).length());
    }

    @Override
    public String toString() {
        String responseLine = String.format("%s %d\r\n", this.version, this.status);
        String data = String.format("\r\n%s", this.data);
        StringBuilder builder = new StringBuilder();
        builder.append(responseLine);
        for (String key : this.header.keySet()) {
            Object value = this.header.get(key);
            builder.append(key).append(": ").append(value.toString()).append("\r\n");
        }
        builder.append(data);
        return builder.toString();
    }

    public byte[] getBytes() {
        return toString().getBytes(StandardCharsets.UTF_8);
    }

}
